package com
{
	import com.be.boulevart.events.FLVRecorderEvent;
	import com.be.boulevart.video.FLVRecorder;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.FileReference;
	import flash.text.TextField;
	import ui.no.Videos;
	//import flash.media.Camera;
	//import flash.media.Video;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class TestCam2flv extends Sprite
	{
		
		public function TestCam2flv():void
		{
			addEventListener(Event.ADDED_TO_STAGE, starts);
		
			trace("=============");
			addChild(txt);
			moveTxt();
		}
		
		private var txt:TextField = new TextField();
		private function moveTxt():void
		{
			txt.x = 0;
			txt.border = true;
			TweenLite.to(txt,1,{onComplete:moveTxt,x:200});
		}
		
		private function stopRec(e:MouseEvent):void
		{
			if (recorder)
				stopRecording();
			else
				startRecording(10, stage);
		
		}
		
		//private var vidDisplay:Video = new Video();
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			//var cam:Camera = Camera.getCamera();
			//addChild(vidDisplay);
			//vidDisplay.attachCamera(cam);
			//vidDisplay.addEventListener(Event.ACTIVATE, starts);
		
		}
		
		private function starts(e:Event):void
		{
			stage.addEventListener(MouseEvent.CLICK, stopRec);
		}
		
		private var tmr:Timer;
		private var recorder:FLVRecorder;
		private var file:ByteArray;
		
		private function startRecording(fps:int, systemManager:Stage):void
		{
			
			if (recorder == null)
			{
				recorder = FLVRecorder.getInstance()
			}
			//file=File.desktopDirectory.resolvePath("recording.flv");
			file = new ByteArray;
			recorder.setTarget(file, stage.stageWidth, stage.stageHeight, fps, systemManager)
			
			if (tmr == null)
			{
				tmr = new Timer(1000 / fps)
			}
			tmr.addEventListener(TimerEvent.TIMER, record);
			tmr.start();
		}
		
		private function record(e:TimerEvent):void
		{
			//recorder.captureComponent(this)   //DisplayObject
			var bmpdata:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
			bmpdata.draw(this);
			recorder.saveFrame(bmpdata); //Bitmapdata
			//recorder. saveSmoothedBitmapFrame(bmp)  //Bitmap
			//recorder. saveSmoothedFrame(bmpdata)  //Bitmapdata
		}
		
		private function stopRecording():void
		{
			tmr.stop()
			
			//when saving is done
			recorder.addEventListener(FLVRecorderEvent.FLV_CREATED, fileMade)
			
			//when saving starts
			recorder.addEventListener(FLVRecorderEvent.FLV_START_CREATION, startCreatingFLV)
			
			recorder.stopRecording();
			
			file.position = 0;
			trace(file.bytesAvailable);
			var files:FileReference = new FileReference();
			files.save(file, FLVRecorder.url);
			txt.visible = false;
			removeChild(txt);
			files.addEventListener(Event.COMPLETE, plays);
		}
		
		private function plays(e:Event):void 
		{
			var vid:Videos = new Videos("e:/test.flv");
			//var vid:Videos = new Videos("e:/1.flv");
			//vid.setSize(0,0,stage.stageWidth,stage.stageHeight);
			addChild(vid).y = 100;
		}
		
		private function fileMade(e:FLVRecorderEvent):void
		{
			trace("com", e.type);
			//recorder.
			//var fileStream:FileStream = new FileStream();
			//fileStream.writeBytes();
			//file
		}
		
		private function startCreatingFLV(e:FLVRecorderEvent):void
		{
			recorder.addEventListener(FLVRecorderEvent.PROGRESS, onFLVCreationProgress)
		}
		
		private function onFLVCreationProgress(e:FLVRecorderEvent):void
		{
			trace(e.progress);
			//e.progress: percent complete (0 to 1)
			//pbSaving: ProgressBar component in Flex
			//pbSaving.setProgress(e.progress,1)
		}
	}
}